package com.ruijie.acl.web.config;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.jboss.seam.jsf.ListDataModel;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.foundation.web.action.BasePagedSupportAction;

/**
 * <p>Title: ACL组选择器件中负责可选ACL的类         </p>
 * <p>Description: Function Description 	</p>
 * <p>Copyright: Copyright (c) 2009     </p>
 * <p>Company: Ruijie Co., Ltd.       </p>
 * <p>Create Time: 2009-09-22           </p>
 * @author ASAI
 * <p>Update Time:                      </p>
 * <p>Updater:                          </p>
 * <p>Update Comments:                  </p>
 */
public class AccessGroupAvailableSelector extends BasePagedSupportAction {

    private AccessGroupService selectorService;

    private AccessGroup accessGroup = new AccessGroup();

    private AccessGroupSelectedSelector selectedSelector;

    /**
     * 查询可供选择设备额外的过滤条件
     */
    private Criterion criterion;
    
    private String sqlCriteriaForSelectAll;

    /**	
     * 可选设备是在浮动层里实现的，若每页显示20条会有UI问题，所以固定每页为10条
     */
    private int[] pageSizeOptions = {10};

    private AccessGroupSelector accessGroupSelector;
    
    public AccessGroupAvailableSelector(AccessGroupSelector accessGroupSelector, AccessGroupService selectorService) {
        this.accessGroupSelector = accessGroupSelector;
        this.selectorService = selectorService;
    }

    @Override
    protected void updateModel() {
        currentPage = selectorService.pagedQueryAvailableAccessGroups(this.selectedSelector.getSelectedAccessGroupList().toArray(),accessGroup, page, pageSize);

        // 如果当前页面已经没有记录(例如删除了当前页面的所有记录)，那么显示上一页
        if (currentPage.getTotalPageCount() < page && page > 1) {
            page--;
            updateModel();
        }

        selectedIds.clear();
    }


    @SuppressWarnings("unchecked")
    ListDataModel getAvailableDataModel() {
        if (null == currentPage){
            updateModel();
        }
        return new ListDataModel((List<AccessGroup>) currentPage.getResult());
    }
    
    @SuppressWarnings("unchecked")
	ListDataModel getAvailableDataModel(Criterion criterion) {
    	this.criterion=criterion;
        if (null == currentPage){
            updateModel();
        }
        return new ListDataModel((List<AccessGroup>) currentPage.getResult());
    }
    
    @Override
    protected int[] getPageSizeOptions() {
       return pageSizeOptions;
    }
    
    public AccessGroup getAccessGroup() {
		return accessGroup;
	}

	public void setAccessGroup(AccessGroup accessGroup) {
		this.accessGroup = accessGroup;
	}

	/**	
     * <p>Description: 选择ACL组,ACL组由页面上的复选框指定 </p>
     * <p>Create Time: Dec 7, 2008   </p>
     * @author lusl
     */
    public void select() {
        select(getIds());
    }

    /**	
     * <p>Description: 显式选择ACL组，由客户端指定选中的ACL组 </p>
     * <p>Create Time: Dec 7, 2008   </p>
     * @author lusl
     * @param ids
     */
    public void select(Long[] ids) {
        List<Long> idList = new ArrayList<Long>();
    	for(Long id : ids){
    		idList.add(id);
    	}
    	this.selectedSelector.getSelectedAccessGroupList().addAll(idList);
        notifyChange();
    }

    /**	
     * <p>Description: 选择所有的ACL组</p>
     * <p>Create Time: Dec 7, 2008   </p>
     * @author lusl
     */
    @SuppressWarnings("unchecked")
    public void selectAll() {
        //selectorService.selectAll(uuid, this.accessGroupSelector.getAllowedDeviceModels(), device, sqlCriteriaForSelectAll);
    	Page page = selectorService.pagedQueryAvailableAccessGroups(this.selectedSelector.getSelectedAccessGroupList().toArray(),accessGroup, 1, 10000);
    	List<AccessGroup> allAccessGroup = (List<AccessGroup>) page.getResult();
    	List<Long> idList = new ArrayList<Long>();
     	for(AccessGroup tAccessGroup : allAccessGroup){
     		idList.add(tAccessGroup.getId());
     	}
     	this.selectedSelector.getSelectedAccessGroupList().addAll(idList);
    	notifyChange();
    }

    public void setSelectedSelector(AccessGroupSelectedSelector selectedSelector) {
        this.selectedSelector = selectedSelector;
    }

    /**	
     * <p>Description:  通知相关的数据模型更新</p>
     * <p>Create Time: Dec 7, 2008   </p>
     * @author lusl
     */
    private void notifyChange() {
        refresh();
        selectedSelector.refresh();
    }

	public Criterion getCriterion() {
		return criterion;
	}

	public void setCriterion(Criterion criterion) {
		this.criterion = criterion;
	}

	public void setSqlCriteriaForSelectAll(String sqlCriteriaForSelectAll) {
		this.sqlCriteriaForSelectAll = sqlCriteriaForSelectAll;
	}
    
	
    

}
